using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.IdentityModel.Tokens;
using Newtonsoft.Json.Linq;
using SovaDataService.SovaDataServices;
using SovaDataService.SovaModels;
using SovaWebService.ViewModels;
using System;
using System.Collections.Generic;
using System.IdentityModel.Tokens.Jwt;
using System.Linq;

namespace SovaWebService.Controllers
{
    [Route("api/weighted_words")]
    public class WeightedWordsController : Controller
    {
        private WeightedWordDataService _dataService;

        /**
         * Main constructor.
         */
        public WeightedWordsController(WeightedWordDataService dataService)
        {
            _dataService = dataService;
        }

        /**
         * Retrieves the weighted words with frequency higher than 5 which relate with the given term.
         */
        [Authorize]
        [HttpGet("{term}")]
        public IActionResult GetWeightedWordsByTerm(string term)
        {
            var words = _dataService.GetRelatedWeightedWords(term)
                        .Where(ww => ww.Frequency > 5);

            var wordList = new WeightedWordListVM();

            foreach (var word in words)
            {
                wordList.Words.Add(new WeightedWordVM()
                {
                    Word = word.Word,
                    Frequency = word.Frequency,
                });
            }
            return Ok(wordList);
        }
    }
}